package com.clock.message.service.impl;

import com.clock.message.dto.NcMessageDto;
import com.clock.message.dto.NcMessageQueryDTO;
import com.clock.message.pojo.NcMessage;
import com.clock.message.service.MessageService;
import com.clock.message.utils.JPushManager;
import com.nineclock.common.enums.ResponseEnum;
import com.nineclock.common.exception.NcException;
import com.nineclock.common.utils.BeanHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.data.mongodb.core.query.UpdateDefinition;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.List;

@Service
@Transactional
public class MessageServiceImpl implements MessageService {
    @Autowired
    private MongoTemplate mongoTemplate;
    @Autowired
    private JPushManager jPushManager;
    @Override
    public void pushMessage(NcMessage message) throws Exception {
        message.setCreateTime(new Date());
        mongoTemplate.save(message);

        jPushManager.sendNotificationByAlias(message.getTitle(), message.getContent(), message.getTargets(), null);
    }

    @Override
    public List<NcMessageDto> getMessageHistory(NcMessageQueryDTO messageQueryDTO) {
        if (messageQueryDTO == null){
            throw new NcException(ResponseEnum.INVALID_PARAM_ERROR);
        }

        Query query = new Query();
        if (!StringUtils.isEmpty(messageQueryDTO.getId())){
            query.addCriteria(new Criteria("_id").is(messageQueryDTO.getId()));
        }
        if (!StringUtils.isEmpty(messageQueryDTO.getType())){
            query.addCriteria(new Criteria("messageType").is(messageQueryDTO.getType()));
        }
        if (!StringUtils.isEmpty(messageQueryDTO.getUserId())){
            query.addCriteria(new Criteria("applyUserId").is(messageQueryDTO.getUserId()));
        }
        if (!StringUtils.isEmpty(messageQueryDTO.getTargetId())){
            query.addCriteria(new Criteria("approveUserId").is(messageQueryDTO.getTargetId()));
        }
        query.with(Sort.by(Sort.Order.asc("approveStatue")));
        List<NcMessage> ncMessageList = mongoTemplate.find(query, NcMessage.class);

        return BeanHelper.copyWithCollection(ncMessageList,NcMessageDto.class);
    }

    @Override
    public void updateMessageWithApproved(String message) {
        Query query = new Query();
        query.addCriteria(new Criteria("_id").is(message));
        Update update = new Update();
        update.set("useStatus",1);
        update.set("approveStatue",1);
        mongoTemplate.updateFirst(query,update,NcMessage.class);
    }

    @Override
    public void refuseMessageWithApproved(String message) {
        Query query = new Query();
        query.addCriteria(new Criteria("_id").is(message));
        Update update = new Update();
        update.set("approveStatue",1);
        mongoTemplate.updateFirst(query,update,NcMessage.class);
    }
}
